<script>
  import $ from '../dfish.js'

  export default {
    props: ['src', 'filter', 'success', 'error', 'complete'],
    data() {
      return {
        loading: false,
        loaded: false,
      }
    },
    methods: {
      getSrc(a) {
        let x = this.x || this.$props
        return a ? $.urlFormat(x.src, x.data, x) : x.src
      },
      request(fn) {
        let src = this.getSrc(true)
        if (src && !this.loading) {
          this.showLoading(true)
          $.ajax({
            src,
            context: this,
            filter: this.x.filter,
            error: this.x.error,
            complete: this.x.complete,
            success: (...args) => {
              this.showLoading(false)
              this.loaded = true
              this.x.success && this.x.success(...args)
              fn && fn()
            }
          })
        }
      },
      showLoading(a) {
        this.loading = a
      }
    }
  }
</script>
